// SIp_BPA.cpp: implementation of the CSIp_BPA class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../../Include/Swi/SIp_BPA.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSIp_BPA::CSIp_BPA()
{
	m_chType = 'S';
	m_chSubType = 'I';
	m_chChangeCode = '+';

	m_dKp = 0;
	m_dT1 = 0;
	m_dT2 = 0;
	m_dT13 = 0;
	m_dT14 = 0;
	m_dT3 = 0;
	m_dT4 = 0;
	m_dVsmax = 0;
	m_dVsmin = 0;
	m_chIB = '0';
	m_dBus_bse = 0;
	m_dKmva = 0;

	memset(m_strBus_Name,' ',8);
	m_strBus_Name[8] = '\0';

	m_strCardType[0] = 'S';
}

CSIp_BPA::CSIp_BPA(CSIp_BPA &sipBPA):CUnit_BPA(sipBPA)
{
	m_dKp = sipBPA.m_dKp;
	m_dT1 = sipBPA.m_dT1;
	m_dT2 = sipBPA.m_dT2;
	m_dT13 = sipBPA.m_dT13;
	m_dT14 = sipBPA.m_dT14;
	m_dT3 = sipBPA.m_dT3;
	m_dT4 = sipBPA.m_dT4;
	m_dVsmax = sipBPA.m_dVsmax;
	m_dVsmin = sipBPA.m_dVsmin;
	m_chIB = sipBPA.m_chIB;
	m_dBus_bse = sipBPA.m_dBus_bse;
	m_dKmva = sipBPA.m_dKmva;

	strcpy(m_strBus_Name,sipBPA.m_strBus_Name);
}

CSIp_BPA::~CSIp_BPA()
{

}

bool CSIp_BPA::FillEx(char *buffer)
{
	int i;

	bool bResult = CUnit_BPA::FillEx(buffer);

	if (!bResult)
	{
		printf("Error in (CSIp_BPA::FillEx): no basic information!\n");
		return false;
	}

	if (strlen(buffer)<17)
		return true;

	m_dKp = StrToDouble(&buffer[16],5,3);
	if(EndOfSec(&buffer[16],5))
		return true;

	m_dT1 = StrToDouble(&buffer[21],5,3);
	if(EndOfSec(&buffer[21],5))
		return true;

	m_dT2 = StrToDouble(&buffer[26],5,3);
	if(EndOfSec(&buffer[26],5))
		return true;

	m_dT13 = StrToDouble(&buffer[31],5,3);
	if(EndOfSec(&buffer[31],5))
		return true;

	m_dT14 = StrToDouble(&buffer[36],5,3);
	if(EndOfSec(&buffer[36],5))
		return true;

	m_dT3 = StrToDouble(&buffer[41],5,3);
	if(EndOfSec(&buffer[41],5))
		return true;

	m_dT4 = StrToDouble(&buffer[46],5,3);
	if(EndOfSec(&buffer[46],5))
		return true;

	m_dVsmax = StrToDouble(&buffer[51],6,4);
	if(EndOfSec(&buffer[51],6))
		return true;

	m_dVsmin = StrToDouble(&buffer[57],6,4);
	if(EndOfSec(&buffer[57],6))
		return true;

	m_chIB = buffer[63];
	if(EndOfSec(&buffer[63],1))
		return true;

	for(i=0;i<8;i++)
	{
		if(!buffer[64+i])
			break;
		else
			m_strBus_Name[i] = buffer[64+i];
	}
	m_strBus_Name[i] = '\0';
	/*if(i!=8)
	{
		printf("Error in (CNode_BPA: FillEx): no node name!\n");
		return false;
	}*/
	if(EndOfSec(&buffer[64],8))
		return true;

	m_dBus_bse = StrToDouble(&buffer[72],4,0);
	if(EndOfSec(&buffer[72],4))
		return true;

	m_dKmva = StrToDouble(&buffer[76],4,0);
	if(EndOfSec(&buffer[76],4))
		return true;

	return true;
}

bool CSIp_BPA::UpdateEx(char *buffer)
{
	bool bResult = CUnit_BPA::UpdateEx(buffer);

	if (!bResult)
	{
		printf("Error in (CSIp_BPA::UpdateEx): impossible!\n");
		return false;
	}

	DoubleToStr(m_dKp,&buffer[16],5,3);
	DoubleToStr(m_dT1,&buffer[21],5,3);
	DoubleToStr(m_dT2,&buffer[26],5,3);
	DoubleToStr(m_dT13,&buffer[31],5,3);
	DoubleToStr(m_dT14,&buffer[36],5,3);
	DoubleToStr(m_dT3,&buffer[41],5,3);
	DoubleToStr(m_dT4,&buffer[46],5,3);
	DoubleToStr(m_dVsmax,&buffer[51],6,4);
	DoubleToStr(m_dVsmin,&buffer[57],6,4);
	buffer[63] = m_chIB;
	strcpy(&buffer[64],m_strBus_Name);
	DoubleToStr(m_dBus_bse,&buffer[72],4,0);
	DoubleToStr(m_dKmva,&buffer[76],4,0); 

	return true;
}